@extends('steward.layoutEdit')

@section('css')
    <link  rel="stylesheet" href="/static/css/steward/order/order.css?v=20171012123123367796">
    <style>
        .col-extend-css {
            width:100%;
            text-align: left;
            padding-left: 32px;
        }
        /*ie7、ie8兼容性*/
        .form-inline button{
            *vertical-align: top;
            *margin-left:5px;
        }
        .form-inline .form-group{
            *display: inline;
            *zoom:1;
        }
        .form-inline .form-group label{
            *display: inline;
            *zoom:1;
        }
        .form-inline .form-group input{
            *width:auto;
            *display: inline;
            *zoom:1;
        }
        .form-control{
            *padding:0;
        }
    </style>
@endsection

@section('title')
    <input type="hidden" id = id value="{{$id}}">
    @if($id == 1)
        <li class="status cur" data_type="1"><span>待接单</span></li>
        <li class="status" data_type="2"><span>待发货</span></li>
        <li class="status" data_type="3"><span>配送中</span></li>
        <li class="status" data_type="4"><span>异常单</span></li>
        <li class="status" data_type="5"><span>催单</span></li>
        <li class="status" data_type="6"><span>退单</span></li>
        <li class="status" data_type="7"><span>已完成</span></li>
        <li class="status" data_type="8"><span>已取消</span></li>
    @else
        <li class="cur"><span>订单列表</span></li>
    @endif
@endsection

@section('content')

    <div class="row">
        <div class="col-lg-12">
            @if($id == 2)
                <div class="row">
                    <div class="col-md-9 col-extend-css">
                        <form class="form-inline" id="search-form" onsubmit="return false;">
                            <div class="form-group">
                                <label for="product_code">会员名：</label>
                                <input type="text" placeholder="请输入会员名" class="form-control" name="coustID" id="custID">&nbsp;&nbsp;
                            </div>
                            <div class="form-group">
                                <label for="barcode">收货人：</label>
                                <input type="text" placeholder="请输入收货人" class="form-control" name="deliver_name" id="deliver_name">&nbsp;&nbsp;
                            </div>
                            <div class="form-group">
                                <label for="product_code">手机号：</label>
                                <input type="text" placeholder="请输入手机号" class="form-control" name="mobile" id="mobile">&nbsp;&nbsp;
                            </div>
                            <div style="height:15px;"></div>
                            <div class="form-group">
                                <label for="barcode">订单号：</label>
                                <input type="text" placeholder="请输入订单号" class="form-control" name="bill_no" id="bill_no">&nbsp;&nbsp;
                            </div>
                            <button class="btn btn-default" id="search" type="button">查询</button>
                            <button class="btn btn-warning" id="re-set" type="button">重置</button>
                        </form>
                    </div>
                </div><br/>
            @endif

            <div class="order-content">
                <div class="top-jilu">共<span class="word-color" id="total"></span>条记录</div>
                <ul class="order-list order-list-detail">
                </ul>
                <div class="position-page" id="limit">
                </div>
            </div>

        </div>
    </div>
@endsection

@section('js')
    <script>

        $(function () {

            var id = $("#id").val();

            $(document).on('click','.status',function(){

                var type = $(this).attr('data_type');
                $(this).addClass('cur').siblings().removeClass('cur');
                var page = $(this).attr('data-paging');
                bill.load_order_list(type,id,page);


            }).on('click','.pagination a', function(){     //分页事件

                var page = $(this).attr('data-paging');
                var type = $(".cur").attr('data_type');

                if (page) {
                    if(id == 1){
                        bill.load_order_list(type,id,page);
                    }else{
                        bill.load_order_list('',id,page);
                    }
                }

            });

            bill.load_order_list(1,id);
            var bill_no = $("#bill_no").val();
            if(id == 2){

                $(document).on('click','#search',function () {

                    var dt = E.getFormValues('search-form');
                    bill.load_order_list('',id,'',dt);

                }).on('click','#re-set',function () {

                    $("#custID").val('');
                    $("#deliver_name").val('');
                    $("#mobile").val('');
                    $("#bill_no").val('');

                    bill.load_order_list('',id); //刷新

                });
            }

        });
        
        //订单操作
        var bill = {
            //页面加载
            load_order_list:function (status,id,page,search) {

                var type       = status ? status : 1;
                var page_log   = page ? page : 1;
                var id_log     = id ? id : 1;

                var lastIndex = layer.load();
                $.ajax({
                    type: 'POST',
                    url: '/lar/steward/order/search/'+id,
                    dataType: 'JSON',
                    data: {
                        'type': type,
                        'page': page_log,
                        'id': id_log,
                        'search': search
                    },
                    timeout: 30000,
                    success: function( res ) {

                        layer.close(lastIndex);
                        $('.order-list').empty();
                        $('#limit').empty();
                        $('#total').html(res.total);

                        if(res.total > 0){
    
                            var html = '';
                            for (var i in res.rows){
                                html += '<li>';
                                html += '<div class="order-status">';
                                html += '<div class="order-top-left">';
                                html += '<span><img src="'+res.rows[i]['platLogo']+'"/></span>';
                                html += '<span><img src="'+res.rows[i]['ziti_logo']+'"/></span>';
                                html += '<p class="text"><a href="#" class="bill_no" type=""  data="'+res.rows[i]['bill_no']+'">订单号：<em class="order-no">'+res.rows[i]['bill_no']+'</em></a></p>';
                                html += '<p class="text">下单时间：<span class="order-time">'+res.rows[i]['create_at']+'</span></p>';
                                html += '</div>';
                                html += '<div class="order-top-right" id="status">'+res.rows[i]['bill_status_name']+'</div>';
                                html += '</div>';
                                html += '<div class="order-info">';
                                html += '<div class="left-info">';
                                html += '<p><span class="name">'+res.rows[i]['deliver_name']+'</span><span class="phone">'+res.rows[i]['deliver_mobile']+'</span><span class="order-num">第'+res.rows[i]['ordernum']+'次下单</span></p>';
                                html += '<p class="address">'+res.rows[i]['adress']+'</p>';
                                html += '<p class="focus-th">期望提货时间：   '+res.rows[i]['date_time']+'</p>';
                                html += '<p class="notice">备注：'+res.rows[i]['remark']+'</p>';
                                if(res.rows[i]['remind'] == 1){
                                    html += '<p class="notice">第：'+res.rows[i]['remind_number']+'次催单</p>';
                                }
                                html += '</div>';
                                html += '<div class="right-info">';
                                html += '<div class="shop-num">商品件数：'+res.rows[i]['bill_total_amount']+'件</div>';
                                html += '<div class="payment">已付金额：<span >￥'+res.rows[i]['bill_totalPaid']+'</span></div>';
                                html += res.rows[i]['add_html'];
                                html += '</div></div></li>';
                            }
                            if(res.link){
                                $("#limit").html(res.link);
                            }
                            $('.order-list').append(html);
                        }
    
                    },
                    error: function( XMLHttpRequest, textStatus ) {
    
                        layer.close(lastIndex);
    
                        switch (textStatus) {
                            case 'timeout':
                                layer.alert('网络爆棚，请重新尝试', {icon: 2, offset: '70px'});
                                break;
    
                            default:
                                layer.alert('系统出错，请重试', {icon: 2, offset: '70px'});
                                break;
                        }
    
                    }
                });
            },

            /*
             * 订单状态的改变
             * bill_no  订单号
             * receive  订单操作标示
             */
            operation:function (bill_no,mark,type) {

                var url = '';

                if(bill_no == ""){
                    layer.alert('订单参数错误，请重试！', {icon: 2, offset: '70px'});
                    return false;
                }

                if(mark == ""){
                    layer.alert('操作参数错误，请重试！', {icon: 2, offset: '70px'});
                    return false;
                }

                if(mark == 'receive'){  //订单接单操作
                    url = '/lar/steward/order/receive';
                }else if(mark == 'packs'){  //订单配货
                    url = '/lar/steward/order/packs';
                }else if(mark == 'packsFinish'){      //订单配货
                    url = '/lar/steward/order/packs_finish';
                }else if(mark == 'hang'){      //订单挂起
                    url = '/lar/steward/order/hang';
                }else if(mark == 'agreRefund'){  //同意申请取消/退单
                    url = '/lar/steward/order/agre_refund';
                }else if(mark == 'cancel_hang'){  //订单取消挂起
                    url = '/lar/steward/order/cancel_hang';
                }else if(mark == 'finish'){  //订单完成
                    url = '/lar/steward/order/finish';
                }
                var lastIndex = layer.load();
                $.ajax({
                    type: 'POST',
                    url: url,
                    dataType: 'JSON',
                    data: {
                        'bill_no': bill_no
                    },
                    timeout: 30000,
                    success: function( res ) {

                        layer.close(lastIndex);
                        layer.msg(res.message,{icon:1,time:1000});

                        bill.refresh(type);//刷新
                    },
                    error: function( XMLHttpRequest, textStatus ) {

                        layer.close(lastIndex);

                        switch (textStatus) {
                            case 'timeout':
                                layer.alert('网络爆棚，请重新尝试', {icon: 2,shade: [0.15, 'black'], offset: '70px'});
                                break;

                            default:
                                layer.alert('系统出错，请重试', {icon: 2,shade: [0.15, 'black'], offset: '70px'});
                                break;
                        }

                    }
                });

            },

            //取消订单
            cancel:function (bill_no,type,index) {

                var val = $(index).parent('div').find("option:selected").text();
                var reason_id = $(index).parent('div').find("option:selected").val();

                if(bill_no == ""){
                    layer.alert('订单参数错误，请重试！', {icon: 2, offset: '70px'});
                    return false;
                }
                if(val == ""){
                    layer.alert('请选择取消原因！', {icon: 2, offset: '70px'});
                    return false;
                }

                var lastIndex = layer.load();
                $.ajax({
                    type: 'POST',
                    url: '/lar/steward/order/cancel',
                    dataType: 'JSON',
                    data: {
                        'bill_no': bill_no,
                        'reason': val,
                        'reasonId': reason_id
                    },
                    timeout: 30000,
                    success: function( res ) {

                        layer.close(lastIndex);
                        if(res.code == 200){
                            layer.msg(res.message,{icon:1,time:1000});
                        }else {
                            layer.msg(res.message,{icon:2,time:1000});
                        }

                        bill.refresh(type);//刷新
                    },
                    error: function( XMLHttpRequest, textStatus ) {

                        layer.close(lastIndex);

                        switch (textStatus) {
                            case 'timeout':
                                layer.alert('网络爆棚，请重新尝试', {icon: 2, offset: '70px'});
                                break;

                            default:
                                layer.alert('系统出错，请重试', {icon: 2, offset: '70px'});
                                break;
                        }

                    }
                });

            },

            //发货
            delivery:function (bill_no,type,index) {

                var val = $(index).parent('div').find('select option:selected').val();

                if(bill_no == ""){
                    layer.alert('订单参数错误，请重试！', {icon: 2, offset: '70px'});
                    return false;
                }

                if(val == ""){
                    layer.alert('请选择配送平台！', {icon: 2, offset: '70px'});
                    return false;
                }

                var lastIndex = layer.load();
                $.ajax({
                    type: 'POST',
                    url: '/lar/steward/order/delivery',
                    dataType: 'JSON',
                    data: {
                        'bill_no': bill_no,
                        'express_id': val
                    },
                    timeout: 30000,
                    success: function( res ) {

                        layer.close(lastIndex);
                        layer.msg(res.message,{icon:1,time:1000});
                        bill.refresh(type);//刷新
                    },
                    error: function( XMLHttpRequest, textStatus ) {

                        layer.close(lastIndex);

                        switch (textStatus) {
                            case 'timeout':
                                layer.alert('网络爆棚，请重新尝试', {icon: 2, offset: '70px'});
                                break;

                            default:
                                layer.alert('系统出错，请重试', {icon: 2, offset: '70px'});
                                break;
                        }

                    }
                });

            },

            //不同意申请取消/退单
            disagreRefund:function (bill_no,type,index) {

                var val = $(index).parent('div').find('.refund').val();

                if(bill_no == ""){
                    layer.alert('订单参数错误，请重试！', {icon: 2, offset: '70px'});
                    return false;
                }

                if(val == ""){
                    layer.alert('请选择拒绝原因！', {icon: 2, offset: '70px'});
                    return false;
                }

                var lastIndex = layer.load();
                $.ajax({
                    type: 'POST',
                    url: '/lar/steward/order/disagre_refund',
                    dataType: 'JSON',
                    data: {
                        'bill_no': bill_no,
                        'cancel_reason': val
                    },
                    timeout: 30000,
                    success: function( res ) {

                        layer.close(lastIndex);
                        layer.msg(res.message,{icon:1,time:1000});
                        bill.refresh(type);//刷新
                    },
                    error: function( XMLHttpRequest, textStatus ) {

                        layer.close(lastIndex);

                        switch (textStatus) {
                            case 'timeout':
                                layer.alert('网络爆棚，请重新尝试', {icon: 2, offset: '70px'});
                                break;

                            default:
                                layer.alert('系统出错，请重试', {icon: 2, offset: '70px'});
                                break;
                        }

                    }
                });
            },

            //回复催单
            remind_reply:function (bill_no,type,index) {

                var val = $(index).parent('div').find('select option:selected').text();

                if(bill_no == ""){
                    layer.alert('订单参数错误，请重试！', {icon: 2, offset: '70px'});
                    return false;
                }

                if(val == "" || val == 'undefined'){
                    layer.alert('请选择回复内容！', {icon: 2, offset: '70px'});
                    return false;
                }

                var lastIndex = layer.load();
                $.ajax({
                    type: 'POST',
                    url: '/lar/steward/order/remind_reply',
                    dataType: 'JSON',
                    data: {
                        'bill_no': bill_no,
                        'reply_message': val
                    },
                    timeout: 30000,
                    success: function( res ) {

                        layer.close(lastIndex);
                        layer.msg(res.message,{icon:1,time:1000});
                        bill.refresh(type);//刷新
                    },
                    error: function( XMLHttpRequest, textStatus ) {
                        layer.close(lastIndex);

                        switch (textStatus) {
                            case 'timeout':
                                layer.alert('网络爆棚，请重新尝试', {icon: 2, offset: '70px'});
                                break;

                            default:
                                layer.alert('系统出错，请重试', {icon: 2, offset: '70px'});
                                break;
                        }

                    }
                });
            },

            //页面刷新
            refresh:function (type) {

                if(type == ""){
                    layer.alert('操作参数错误，请重试！', {icon: 2, offset: '70px'});
                    return false;
                }

                bill.load_order_list(type,1,1);
            }

        };
        //查看订单详情
        $(document).on('click','.bill_no',function(){
                    var bill_no=$(this).attr('data');
                    var bill_status=$('#status').html();
                    layer.open({
                        title:false,
                        type : 2,
                        area : ['100%','100%'],
                        closeBtn:0 ,
                        content : '/lar/steward/order/detail/'+bill_no+'/'+bill_status
                    })
                });

        /**
         * 调取PC客户端执行打印
         * @param order_id int 订单号
         */
        function client_print(order_id) {

            parent.client_print(order_id);

        }


    </script>

@endsection
